<?php
namespace Company\Controller;
use Think\Controller;
class RoleController extends BaseController {
    public function _initialize()
    {
        parent::_initialize();
        $g=session('agent');
        $db_name=$g['db'];
        $this->product = M('role',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
    }

    public function admin(){
        if(I("get.sort")!=''){
            $order=C('DB_PREFIX_T')."role.".I("get.sort")." ".I("get.dec");
        }else{
            $order=C('DB_PREFIX_T').'role.id desc';
        }
        $list = $this->product->field(C('DB_PREFIX_T').'role.*,'.C('DB_PREFIX_T').'department.name as d_name')
            ->join(C('DB_PREFIX_T')."department on ".C('DB_PREFIX_T')."department.id=".C('DB_PREFIX_T')."role.department_id",'left')
            ->order($order)
            ->select();
        $this->assign('list',$list);
        $this->display();
    }
    public function admin_add(){
        $this->display();
    }
    public function admin_addD($name,$username,$password,$repassword,$department_id){
        if($name==''){
            alert(0,'姓名');
        }
        if($username==''){
            alert(0,'用户名不能为空');
        }
        if($password==''){
            alert(0,'密码不能为空');
        }
        if($password!=$repassword){
            alert(0,'两次密码输入不相同');
        }
        if($department_id==''){
            alert(0,'部门不能为空');
        }
        $count=$this->product->where("username='".$username."'")->count();
        if($count>0){
            alert(0,'用户名已经存在');
        }
        $data['name']=$name;
        $data['username']=$username;
        $data['salt']=rand(111111,999999);
        $data['password']=md5($password.$data['salt']);
        $data['department_id']=$department_id;
        $massage=session('agent');
        $data['operator']=$massage['username'];
        $result=$this->product->add($data);
        if($result){
            alert(1,'添加成功');
        }else{
            alert(0,'添加失败');
        }
    }
    public function admin_edit($id){
        if($id==''){
            echo "错误";
        }
        $this->data=$this->product->find($id);
        $this->display();
    }
    public function admin_editD($id,$name,$password,$repassword,$department_id){
        if($name==''){
            alert(0,'姓名');
        }
        if($password!=$repassword){
            alert(0,'两次密码输入不相同');
        }
        if($department_id==''){
            alert(0,'部门不能为空');
        }
        if($password!=''){
            $data['salt']=rand(111111,999999);
            $data['password']=md5($password.$data['salt']);
        }
        $data['name']=$name;
        $data['department_id']=$department_id;
        $massage=session('agent');
        $data['operator']=$massage['username'];
        $result=$this->product->where("id='".$id."'")->save($data);
        if($result){
            alert(1,'修改成功');
        }else{
            alert(0,'修改失败');
        }
    }
    public function admin_del($id){
        if($id==''){
            alert(0,'操作有误');
        }
        ///////$id=1@2@3
        $group_id=explode('@',$id);
        $id=implode(',',$group_id);
        $result=$this->product->delete($id);
        if($result){
            alert(1,'成功删除了'.$result.'条数据，共'.count($group_id).'条');
        }else{
            alert(0,'删除失败');
        }
    }


    protected function department_ini(){
        $g=session('agent');
        $db_name=$g['db'];
        $this->product = M('department',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
    }


    public function department(){
        $this->department_ini();
        $f=C('DB_PREFIX_T');
        if(I("get.sort")=='name' || I("get.sort")=='id' || I("get.sort")=='operator'){
            $order=$f.'department.'.I("get.sort")." ".I("get.dec");
        }elseif(I("get.sort")=='count') {
            $order=I("get.sort")." ".I("get.dec");
        } else
        {
            $order=$f.'department.id desc';
        }
        $list = $this->product->field($f."department.*,count(".$f."role.id) as count")->join($f."role on ".$f."role.department_id=".$f."department.id",'left')
            ->group($f."department.id")->order($order)->select();
        $this->assign('list',$list);
        $this->display();
    }
    public function department_add(){
		$g=session('agent');
        $db_name=$g['db'];
		$root=M('root',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name)->order("sort")->select();
        $new_arr=array();
		foreach($root as $k=>$v){
            if($v['pid']==0){//顶级菜单
                foreach($root as $j=>$u){
					if($v['id']==$u['pid']){
						foreach($root as $js=>$us){
							if($u['id']==$us['pid']){
								foreach($root as $jss=>$uss){
									if($us['id']==$uss['pid']){
											$us['child'][]=$uss;
									}
								}
								$u['child'][]=$us;
							}
						}
						$v['child'][]=$u;
					}
				}
				$new_arr[]=$v;
            }
        }
		// dump($new_arr);
		// die;
		$this->assign('list',$new_arr);
        $this->display();
    }
    public function department_addD($name,$group_id){
        $g=session('agent');
        $db_name=$g['db'];
        $this->p = M('root',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        if($name==''){
            alert(0,'部门名称为空');
        }
        if($group_id==''){
            alert(0,'权限不能为空');
        }
        $count=$this->p->where("name='".$name."'")->count();
        if($count>0){
            alert(0,'名称已经存在');
        }
        //////权限检查，是否具备这些权限
        $arr_id=explode(',',$group_id);
        foreach($arr_id as $v){
            if(!is_numeric($v)){
                alert('操作有误');
            }
        }
        $where['id']=array('IN',$arr_id);
        $result=$this->p->where($where)->getField('name',true);
        if(count($result)!=count($arr_id)){
            alert('操作有误');
        }
        $data['name']=$name;
        $massage=session('agent');
        $data['operator']=$massage['username'];
        $this->department_ini();
        $data['root_group_id']=$group_id;
        $result=$this->product->add($data);
        if($result){
            alert(1,'添加成功');
        }else{
            alert(0,'添加失败');
        }
    }
    public function department_edit($id){
        $this->department_ini();
        if($id==''){
            echo "错误";
        }
        $this->data=$this->product->find($id);
		// dump($this->data);
		$g=session('agent');
        $db_name=$g['db'];
		$root=M('root',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name)->order("sort")->select();
		$this->product = M('department',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $root_id=$this->product->where(array('id'=>$id))->getField('root_group_id');
        $arr_root=explode(',',$root_id);
        $new_arr=array();
		foreach($root as $k=>$v){
			if(in_array($v['id'],$arr_root)){
				$v['checked']=1;
			}
            if($v['pid']==0){//顶级菜单
                foreach($root as $j=>$u){
					if(in_array($u['id'],$arr_root)){
						$u['checked']=1;
					}
					if($v['id']==$u['pid']){
						foreach($root as $js=>$us){
							if(in_array($us['id'],$arr_root)){
								$us['checked']=1;
							}
							if($u['id']==$us['pid']){
								foreach($root as $jss=>$uss){
									if(in_array($uss['id'],$arr_root)){
										$uss['checked']=1;
									}
									if($us['id']==$uss['pid']){
											$us['child'][]=$uss;
									}
								}
								$u['child'][]=$us;
							}
						}
						$v['child'][]=$u;
					}
				}
				$new_arr[]=$v;
            }
        }
		// dump($new_arr);
		// die;
		$this->assign('list',$new_arr);
        $this->display();
    }
    public function department_editD($id,$group_id){
        $g=session('agent');
        $db_name=$g['db'];
        $this->p = M('root',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        if($group_id==''){
            alert(0,'权限不能为空');
        }
        //////权限检查，是否具备这些权限
        $arr_id=explode(',',$group_id);
		// dump($arr_id);
		// die;
        foreach($arr_id as $v){
            if(!is_numeric($v)){
                alert('操作有误');
            }
        }
        $where['id']=array('IN',$arr_id);
        $result=$this->p->where($where)->getField('name',true);
        if(count($result)!=count($arr_id)){
            alert('操作有误');
        }
        $massage=session('agent');
        $data['operator']=$massage['username'];
        $this->department_ini();
        $data['root_group_id']=$group_id;
        $result=$this->product->where(array('id'=>$id))->save($data);
        if($result){
            alert(1,'修改成功');
        }else{
            alert(0,'修改失败');
        }
    }
    public function department_del($id){
        $this->department_ini();
        if($id==''){
            alert(0,'操作有误');
        }
        ///////$id=1@2@3
        $g=session('agent');
        $db_name=$g['db'];
        $this->p = M('role',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $count=$this->p->where(array('department_id'=>$id))->count();
        if($count>0){
            alert(0,'该组存在管理员，不允许删除');
        }
        $group_id=explode('@',$id);
        $id=implode(',',$group_id);
        $result=$this->product->delete($id);
        if($result){
            alert(1,'成功删除了'.$result.'条数据，共'.count($group_id).'条');
        }else{
            alert(0,'删除失败');
        }
    }
    public function getRole(){
        $g=session('agent');
        $db_name=$g['db'];
        $this->product = M('root',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $result=$this->product->select();
        $top=array();
        foreach($result as $k=>$v){
            if($v['is_root']==1){
                $top[]=$v;
                unset($result[$k]);
            }
        }
        foreach($top as $k=>$v){
            foreach($result as $m=>$u){
                if($u['controller']==$v['controller']){
                    $top[$k]['child'][]=$u;
                    unset($result[$m]);
                }
            }
        }
        if($top){
            alert(1,$top);
        }else{
            alert(0,'暂无');
        }
    }
    public function getEditRole($id){
        $g=session('agent');
        $db_name=$g['db'];
        $this->product = M('root',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $result=$this->product->select();
        $this->product = M('department',C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . $db_name);
        $root_id=$this->product->where(array('id'=>$id))->getField('root_group_id');
        $arr_root=explode(',',$root_id);
        $top=array();
        foreach($result as $k=>$v){
            if($v['is_root']==1){
                if(in_array($v['id'],$arr_root)){
                    $result[$k]['checked']=1;
                }
                $top[]=$result[$k];
                unset($result[$k]);
            }
        }
        foreach($top as $k=>$v){
            foreach($result as $m=>$u){
                if($u['controller']==$v['controller']){
                    if(in_array($u['id'],$arr_root)){
                        $result[$m]['checked']=1;
                    }
                    $top[$k]['child'][]=$result[$m];
                    unset($result[$m]);
                }
            }
        }
        if($top){
            alert(1,$top);
        }else{
            alert(0,'暂无');
        }
    }
    public function getDepartment(){
        $this->department_ini();
        $result=$this->product->select();
        if($result){
            alert(1,$result);
        }else{
            alert(0,'暂无');
        }
    }
}